(()=>{
       //百度地图获取
   var map=new BMap.Map("allmap");
   var address="江苏省南京市";
   var pointip1=118.842144;
   var pointip2=32.234554;
   var mapType1 = new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP]});

var mapType2 = new BMap.MapTypeControl({anchor: BMAP_ANCHOR_TOP_LEFT});
  /*
        初始化地图
         */
         
   map.setMapStyle({
       styleJson:[
           {
               //不显示点信息
               "featureType":"poi",
               "elementType":"all",
               "stylers":{
                   "color":"#fff",
                   "visibility":"off"//能见度
               }
           }
       ]
   });
   //map.disableDraggle();//禁止拖动
   //map.disableDoubleClickZoom();//禁止双击缩放
   map.enableScrollWheelZoom();//鼠标控制缩放
   map.addControl(mapType1);          //2D图，卫星图

map.addControl(mapType2);          //左上角，默认地图控件
   var blist=[];
   var districtLoading=0;
   
   var point = new BMap.Point(118.798877,31.939273);
	var circle = new BMap.Circle(new BMap.Point(118.798877,31.939273),5000,{strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5}); //创建圆
	map.addOverlay(circle);            //增加圆
   
//    function getBoundary(){
    
//        addDistrict(address)
//    }
   //添加行政区划，param{}districtName行政区名 ，returns无返回值
   function addDistrict(districtName){
       //使用计数器来控制加载过程
       districtLoading++;
       var bdary = new BMap.Boundary();
    bdary.get(districtName, function (rs) {       //获取行政区域
        var count = rs.boundaries.length; //行政区域的点有多少个
        if (count === 0) {
            alert('未能获取当前输入行政区域');
            return;
        }
        for (var i = 0; i < count; i++) {
            blist.push({ points: rs.boundaries[i], name: districtName });
          
        };
        //console.log(blist);
        //加载完成区域点后计数器-1
        districtLoading--;
        if (districtLoading == 0) {
            //全加载完成后画端点
            drawBoundary();
        }
    });
   }
   /**
 * 各种鼠标事件绑定
 */
function click(evt) {
   
}

function mouseover(evt) {
    evt.target.label.setZIndex(99);
    evt.target.label.setPosition(evt.point);
    //evt.target.label.show();
    //console.log(evt)
}

function mousemove(evt) {
    evt.target.label.setPosition(evt.point);
}

function mouseout(evt) {
    evt.target.label.hide();
}

var point =  new BMap.Point( 113.347655, 23.160435); 
map.centerAndZoom(point,  15); 

function drawcircle(point1,point2,radius){//画圆
    var radius2=0.75*radius
    var radius3=0.5*radius
    var radius4=0.25*radius
    
    
    var circle = new BMap.Circle(new BMap.Point(point1,point2),radius,{strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle1 = new BMap.Circle(new BMap.Point(point1,point2),radius2,{strokeColor:"red", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle2 = new BMap.Circle(new BMap.Point(point1,point2),radius3,{strokeColor:"orange", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle3 = new BMap.Circle(new BMap.Point(point1,point2),radius4,{strokeColor:"green", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    map.addOverlay(circle); 
    map.addOverlay(circle1);    
    map.addOverlay(circle2);
    map.addOverlay(circle3);
    //var line1=new BMap.Polyline([new BMap.Point(point1,point2),new BMap.Point(point1,point2+radius/63781.37/1.8),{strokeColor:"red",strokeWeight:3,strokeOpacity:0.6}])
    //map.addOverlay(line1);
    
}
//画小红点
function drawpoint(point1,point2){
    var point = new BMap.Point(point1,point2);
	//map.centerAndZoom(point, 15);
	var marker = new BMap.Marker(point);  // 创建标注
	map.addOverlay(marker);               // 将标注添加到地图中
}
//画纯文字信息窗口
function drawopts(point1,point2,tit,text){
    var point = new BMap.Point(point1,point2);
    var marker = new BMap.Marker(point);
    map.addOverlay(marker); 
    var opts = {
        width : 200,     // 信息窗口宽度
        height: 100,     // 信息窗口高度
        title :tit , // 信息窗口标题
        enableMessage:true,//设置允许信息窗发送短息
        message:"亲耐滴，晚上一起吃个饭吧？戳下面的链接看下地址喔~",
      }
      var infoWindow = new BMap.InfoWindow(text, opts);  // 创建信息窗口对象 
      marker.addEventListener("click", function(){          
		map.openInfoWindow(infoWindow,point); //开启信息窗口
    });

	
}
//添加图片
function addpic(point1,point2){
    var point = new BMap.Point(point1,point2);
	//map.centerAndZoom(point, 15);
	
	//创建小狐狸
	var pt = new BMap.Point(point1,point2);
	var myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/fox.gif", new BMap.Size(300,157));
	var marker2 = new BMap.Marker(pt,{icon:myIcon});  // 创建标注
	//map.addOverlay(marker2);              // 将标注添加到地图中
}
function drawline(point1,point2){
    //var line1=new BMap.Polyline([new BMap.Point(point1,point2),new BMap.Point(point1,point2+60000/63781.37/1.8),{strokeColor:"red",strokeWeight:3,strokeOpacity:0.6}])
    //map.addOverlay(line1);
}
   function drawBoundary() {
       //清除地图覆盖物
    map.clearOverlays();
  
    //包含所有区域的点数组
    var pointArray = [];

    /*画遮蔽层的相关方法
    *思路: 首先在中国地图最外画一圈，圈住理论上所有的中国领土，然后再将每个闭合区域合并进来，并全部连到西北角。
    *      这样就做出了一个经过多次西北角的闭合多边形*/
    //定义中国东南西北端点，作为第一层
    var pNW = { lat: 59.0, lng: 73.0 }
    var pNE = { lat: 59.0, lng: 136.0 }
    var pSE = { lat: 3.0, lng: 136.0 }
    var pSW = { lat: 3.0, lng: 73.0 }
    //向数组中添加一次闭合多边形，并将西北角再加一次作为之后画闭合区域的起点
    var pArray = [];
    pArray.push(pNW);
    pArray.push(pSW);
    pArray.push(pSE);
    pArray.push(pNE);
    pArray.push(pNW);
    //循环添加各闭合区域
    for (var i = 0; i < blist.length; i++) {
        //添加显示用标签层
        var label = new BMap.Label(blist[i].name, { offset: new BMap.Size(20, -10) });
        label.hide();
        map.addOverlay(label);

        //添加多边形层并显示
        var ply = new BMap.Polygon(blist[i].points, { strokeWeight: 5, strokeColor: "#FF0000", fillOpacity: 0.01, fillColor: " #FFFFFF" }); //建立多边形覆盖物
        ply.name = blist[i].name;
        ply.label = label;
        ply.addEventListener("click", click);
        ply.addEventListener("mouseover", mouseover);
        ply.addEventListener("mouseout", mouseout);
        ply.addEventListener("mousemove", mousemove);
        map.addOverlay(ply);

        //添加名称标签层
        //var centerlabel = new BMap.Label(blist[i].name, { offset: new BMap.Size(0, 0) });
        //centerlabel.setPosition(ply.getBounds().getCenter());
        //map.addOverlay(centerlabel);

        //将点增加到视野范围内
        var path = ply.getPath();
        pointArray = pointArray.concat(path);
        //将闭合区域加到遮蔽层上，每次添加完后要再加一次西北角作为下次添加的起点和最后一次的终点
        pArray = pArray.concat(path);
        pArray.push(pArray[0]);
    }
    

    //限定显示区域，需要引用api库
    var boundply = new BMap.Polygon(pointArray);
    BMapLib.AreaRestriction.setBounds(map, boundply.getBounds());
    map.setViewport(pointArray);    //调整视野 

    //添加遮蔽层
    var plyall = new BMap.Polygon(pArray, { strokeOpacity: 0.0000001, strokeColor: "#000000", strokeWeight: 0.00001, fillColor: "#000000", fillOpacity: 0.4 }); //建立多边形覆盖物
    map.addOverlay(plyall);

    map.enableScrollWheelZoom();
    
    var point = new BMap.Point(118.798877,31.939273);
	//map.centerAndZoom(point, 15);
    /*var circle = new BMap.Circle(new BMap.Point(118.798877,31.939273),85000,{strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle1 = new BMap.Circle(new BMap.Point(118.798877,31.939273),50000,{strokeColor:"red", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle2 = new BMap.Circle(new BMap.Point(118.798877,31.939273),30000,{strokeColor:"orange", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle3 = new BMap.Circle(new BMap.Point(118.798877,31.939273),10000,{strokeColor:"green", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    map.addOverlay(circle);    
    map.addOverlay(circle1);
    map.addOverlay(circle2);    
    map.addOverlay(circle3);
    

    var circle = new BMap.Circle(new BMap.Point(117.214094,39.17241),85000,{strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle1 = new BMap.Circle(new BMap.Point(117.214094,39.17241),50000,{strokeColor:"red", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle2 = new BMap.Circle(new BMap.Point(117.214094,39.17241),30000,{strokeColor:"orange", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    var circle3 = new BMap.Circle(new BMap.Point(117.214094,39.17241),10000,{strokeColor:"green", strokeWeight:3, strokeOpacity:0.5}); //创建圆
    map.addOverlay(circle); 
    map.addOverlay(circle1);    
    map.addOverlay(circle2);
    map.addOverlay(circle3);*/
    drawcircle(118.798877,31.939273,85000)
    //drawpoint(118.798877,31.939273,85000)
    addpic(118.798877,31.939273)
    drawopts(118.798877,31.939273,"观测点01","设备id号：1234，经纬度：118.798877,31.939273")
    drawcircle(117.214094,39.17241,60000)
    //drawpoint(117.214094,39.17241,60000)
    //drawopts(117.214094,39.17241,60000,11)
    
    

}

    addDistrict(address);
    $("#pBox").on("click","a",function(res){
        //console.log(this)
        var city=$(this).html();
        
        $(".csactive").not(this).removeClass("csactive")
        $(this).closest('div').addClass("csactive")
        //console.log(cs)
        console.log(city)
        adress=city;
        blist=[];
        
        addDistrict(adress);
    })

    

})()